<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function getData (url) {
      return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
              return resolve(xhr.responseText);//成功
            } else {
              return reject(xhr.responseText);//失败
            }
          }
        }
        xhr.open("GET", url)
        xhr.send();
      })
    }


    // let promise = getData("http://localhost:3008/api/student/getstudent")
    // promise.then((result) => {
    //   console.log(result);
    // }).catch((error) => {
    //   console.log(error);
    // })

    function postParamsData (url, data) {
      return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
              return resolve(xhr.responseText);//成功
            } else {
              return reject(xhr.responseText);//失败
            }
          }
        }
        xhr.open("POST", url)
        //请求数据:表单类型或者窗体格式: username=admin&password=123
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(data);
      })
    }
    // postParamsData("http://localhost:3008/api/student/removeStudent", "id=1714445619432").then(result => {
    //   console.log(result);
    // }).catch(error => {
    //   console.log(error);
    // })

    function postJSONData (url, data) {
      return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4) {
            if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
              return resolve(xhr.responseText);//成功
            } else {
              return reject(xhr.responseText);//失败
            }
          }
        }
        xhr.open("POST", url)
        //请求数据类 JSON:{"username":"admin","password":"123"}
        xhr.setRequestHeader("token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNzE1MzIyNDc2LCJleHAiOjE3MTUzNzI1NDF9.1DitdTyoMbk6WjxbFjv8jF7bra3mSbUlm0dVa61TWToJu7N86FUdNVjeuthrAi4241grzl7Kg1M91mPmw6jP9Q")
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.send(data);
      })
    }
    let p = {
      status: "在职",
      name: ""
    }
    postJSONData("http://192.168.2.57:8090/employee/lists", JSON.stringify(p))
      .then(result => {
        console.log(result);
      }).catch(error => {
        console.log(error);
      })


  </script>
</body>

</html>